package cn.yh.register.hosp.controller;

import cn.yh.register.common.result.Result;
import cn.yh.register.common.util.MD5;
import cn.yh.register.hosp.service.HospitalSetService;
import cn.yh.register.model.hosp.HospitalSet;
import cn.yh.register.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

//@CrossOrigin //解决跨域问题
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 查询医院设置的所有表信息
     * @return
     */
    @ApiOperation("查询所有")
    @GetMapping("/findAll")
    public Result<List<HospitalSet>> findAll(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    //根据id删除医院设置的信息
    @ApiOperation("根据id删除")
    @ApiImplicitParam(name = "id",value = "主键id",required = true,dataType = "Long",paramType = "path")
    @DeleteMapping("/deleteId/{id}")
    public Result<?> deleteById(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if (flag){
            return Result.ok();
        }
        return Result.fail();
    }

    // 带条件的分页查询
    @ApiOperation("带条件的分页查询")
    @PostMapping("/findPage/{current}/{limit}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页current", required = true, dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "limit", value = "当前页大小", required = true, dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "queryVo",value = "视图对象",required = false,dataType = "HospitalSetQueryVo",paramType = "body")
    })
    public Result<Page<HospitalSet>> findPage(@PathVariable Integer current, @PathVariable Integer limit,
                                       @RequestBody(required = false) HospitalSetQueryVo queryVo){
       Page<HospitalSet> page = new Page<HospitalSet>(current,limit);
        Page<HospitalSet> hospitalSetPage = null;
       if (queryVo != null){
           LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<>();
           wrapper.like(!StringUtils.isEmpty(queryVo.getHosname()),HospitalSet::getHosname,queryVo.getHosname())
                   .eq(!StringUtils.isEmpty(queryVo.getHoscode()),HospitalSet::getHoscode,queryVo.getHoscode());
           hospitalSetPage  = hospitalSetService.page(page, wrapper);
       }else{
           hospitalSetPage = hospitalSetService.page(page, null);
       }
       return Result.ok(hospitalSetPage);
    }

    // 添加医院设置的信息
    @ApiOperation("添加医院设置")
    @PostMapping("/saveHospitalSet")
    @ApiImplicitParam(name = "hospitalSet",value = "医院设置对象",required = true,dataType = "HospitalSet",paramType = "body")
    public Result<?> saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置签名key  MD5(系统毫秒值+1000以内的随机数)
        String signKey = MD5.encrypt(System.currentTimeMillis()+""+new Random().nextInt(100));
        hospitalSet.setSignKey(signKey);
        //设置status   /设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        boolean flag = hospitalSetService.save(hospitalSet);
        if (flag){
            return Result.ok();
        }
        return Result.fail();
    }

    // 根据id查询医院管理设置的信息
    @ApiOperation("根据id查询医院设置")
    @GetMapping("/getHospSet/{id}")
    @ApiImplicitParam(name = "id",value = "id查询医院管理",required = true,dataType = "Long",paramType = "path")
    public Result<HospitalSet> getHospSet(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    // 修改医院管理设置
    @ApiOperation("修改医院设置")
    @PostMapping("/updateHospitalSet")
    @ApiImplicitParam(name = "hospitalSet",value = "修改医院设置",required = true,dataType = "HospitalSet",paramType = "body")
    public Result<?> updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag){
            return Result.ok();
        }
        return Result.fail();
    }

    //批量删除医院管理
    @ApiOperation("批量删除医院设置")
    @DeleteMapping("/batchRemoveHospitalSet")
    public Result<?> batchRemoveHospitalSet(@RequestBody List<Long> ids){
        hospitalSetService.removeByIds(ids);
        return Result.ok();
    }

    // 解锁 锁定
    @ApiOperation("状态解锁/锁定")
    @PostMapping("/lockHospitalSet/{id}/{status}")
    public Result<?> lockHospitalSet(@PathVariable Long id,@PathVariable Integer status){
        HospitalSet hospitalSet = new HospitalSet();
        hospitalSet.setId(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    // 发送签名key
    @ApiOperation("发送签名key")
    @PostMapping("/sendSignKey/{id}")
    public Result<?> sendSignKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //医院编号
        String hosCode = hospitalSet.getHoscode();
        //签名key
        String signKey = hospitalSet.getSignKey();
        //TODO 发送签名key
        return Result.ok();
    }
}
